﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.JointVariableLimitsEditor.CCoJVLEditorCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using COMMANDPARAMSLib;
using EngineeringInternalExtension;
using EngineeringInternalExtension.CommandParameters;
using System.Collections.Generic;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.JointVariableLimitsEditor;

public class CCoJVLEditorCmd : ITxCommand, ITxExecutableCommand
{
  private ITxCommandEnabler m_enabler = (ITxCommandEnabler) new TxCommandEnabler(true);

  public ITxCommandEnabler CommandEnabler => this.m_enabler;

  public bool Connect() => true;

  public void Disconnect()
  {
  }

  public void Execute(object cmdParams)
  {
    TxJointVariableLimitsParametersEx paramsByCmdParams = this.getVarLimitsParamsByCmdParams(cmdParams);
    if (paramsByCmdParams == null)
      return;
    if (((Form) new CUiJVLEditorForm()
    {
      VarLimitsParams = (object) paramsByCmdParams
    }).ShowDialog() != DialogResult.OK)
      return;
    this.applyVarLimitsParamsChangesToCmdParams(paramsByCmdParams, cmdParams);
  }

  public bool Expose => true;

  public string Identifier => "JointVariableLimitsEditor.CCoJVLEditorCmd";

  private TxJointVariableLimitsParametersEx getVarLimitsParamsByCmdParams(object cmdParams)
  {
    switch (cmdParams)
    {
      case ICoJointVariableLimitsParams variableLimitsParams:
        pattern_0 = new TxJointVariableLimitsParametersEx();
        if (variableLimitsParams.EditedJointName != null)
          pattern_0.EditedJointName = variableLimitsParams.EditedJointName;
        TxJoint.TxJointType txJointType = (TxJoint.TxJointType) 1;
        switch (variableLimitsParams.EditedJointType)
        {
          case 0:
            txJointType = (TxJoint.TxJointType) 1;
            break;
          case 1:
            txJointType = (TxJoint.TxJointType) 2;
            break;
        }
        pattern_0.EditedJointType = txJointType;
        TxJoint txJoint = this.gtObjectFromApiObj(variableLimitsParams.Joint) as TxJoint;
        if (TxJoint.op_Inequality(txJoint, (ITxObject) null))
          pattern_0.Joint = txJoint;
        double[] limitsLeadValues = variableLimitsParams.VariableLimitsLeadValues;
        double[] limitsDependValues = variableLimitsParams.VariableLimitsDependValues;
        if (limitsLeadValues != null && limitsDependValues != null)
        {
          pattern_0.VariableLimitsLeadValues = new List<double>((IEnumerable<double>) limitsLeadValues);
          pattern_0.VariableLimitsDependValues = new List<double>((IEnumerable<double>) limitsDependValues);
          break;
        }
        break;
    }
    return pattern_0;
  }

  private void applyVarLimitsParamsChangesToCmdParams(
    TxJointVariableLimitsParametersEx varLimitsParams,
    object cmdParams)
  {
    if (varLimitsParams == null || cmdParams == null || varLimitsParams == cmdParams)
      return;
    switch (cmdParams)
    {
      case ICoJointVariableLimitsParams _:
        ICoJointVariableLimitsParams variableLimitsParams = cmdParams as ICoJointVariableLimitsParams;
        variableLimitsParams.VariableLimitsLeadValues = varLimitsParams.VariableLimitsLeadValues.ToArray();
        variableLimitsParams.VariableLimitsDependValues = varLimitsParams.VariableLimitsDependValues.ToArray();
        break;
      case TxJointVariableLimitsParametersEx _:
        TxJointVariableLimitsParametersEx limitsParametersEx = cmdParams as TxJointVariableLimitsParametersEx;
        limitsParametersEx.VariableLimitsLeadValues = varLimitsParams.VariableLimitsLeadValues;
        limitsParametersEx.VariableLimitsDependValues = varLimitsParams.VariableLimitsDependValues;
        break;
    }
  }

  private ITxObject gtObjectFromApiObj(ITApiObj apiObj)
  {
    string str;
    apiObj.DumpToString(ref str);
    ITxObject itxObject = (ITxObject) null;
    if (str != null)
      itxObject = TxConvertor.tID2Object(str);
    return itxObject;
  }
}
